﻿
/*----------------------------------------------------------------
 * 创建时间：2023/8/4 9:30:29
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Core.Contrls 
 * 描  述：箭头连线辅助类
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Minotaur.App.Core.Contrls
{
    /// <summary>
    /// 箭头连线辅助类
    /// </summary>
    public class FlowArrowLineUits
    {
        /// <summary>
        /// 父级画布
        /// </summary>
        public Canvas ParentCanvas { get; set; }
        public Canvas ArrowCanvas { get; set; }

        /// <summary>
        /// 开始节点ID
        /// </summary>
        public long StartId { get; set; }

        /// <summary>
        /// 结束节点ID
        /// </summary>
        public long EndId { get; set; }

        /// <summary>
        /// 画箭头和连线
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <param name="arrowAngle"></param>
        /// <param name="arrowLength"></param>
        /// <returns></returns>
        public Polygon DrawArrow(double x1, double y1, double x2, double y2, double arrowAngle = Math.PI / 12, double arrowLength = 20)
        {
            Point point1 = new Point(x1, y1);     // 箭头起点
            Point point2 = new Point(x2, y2);     // 箭头终点
            double angleOri = Math.Atan((y2 - y1) / (x2 - x1));      // 起始点线段夹角
            double angleDown = angleOri - arrowAngle;   // 箭头扩张角度
            double angleUp = angleOri + arrowAngle;     // 箭头扩张角度
            int directionFlag = (x2 > x1) ? -1 : 1;     // 方向标识
            double x3 = x2 + ((directionFlag * arrowLength) * Math.Cos(angleDown));   // 箭头第三个点的坐标
            double y3 = y2 + ((directionFlag * arrowLength) * Math.Sin(angleDown));
            double x4 = x2 + ((directionFlag * arrowLength) * Math.Cos(angleUp));     // 箭头第四个点的坐标
            double y4 = y2 + ((directionFlag * arrowLength) * Math.Sin(angleUp));
            Point point3 = new Point(x3, y3);   // 箭头第三个点
            Point point4 = new Point(x4, y4);   // 箭头第四个点
            Point[] points = new Point[] { point1, point2, point3, point4, point2 };   // 多边形，起点 --> 终点 --> 第三点 --> 第四点 --> 终点
            Polygon myPolygon = new Polygon
            {
                Stroke = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xA5, 0x00)),
                StrokeThickness = 2,      // 多边形线宽
                StrokeLineJoin = PenLineJoin.Round,
                Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xA5, 0x00)),   // 填充
            };
            for (int i = 0; i < points.Length; i++)
            {
                myPolygon.Points.Add(points[i]);
            }
            return myPolygon;
        }

        /// <summary>
        /// 添加新的连线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void NewDrawArrowLine(Point startPoint)
        {
            if (this.ParentCanvas.Children.Contains(this.ArrowCanvas)) return;          

            // 多边形，起点 --> 终点 --> 第三点 --> 第四点 --> 终点
            Polygon myPolygon = new Polygon
            {
                Stroke = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xA5, 0x00)),
                StrokeThickness = 10,
                StrokeLineJoin = PenLineJoin.Round,
                Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xA5, 0x00)),
                Points = new PointCollection
                {
                    startPoint,
                    startPoint,
                    startPoint,
                    startPoint,
                    startPoint,
                },
            };
            this.ArrowCanvas.Children.Add(myPolygon); 
        }
        /// <summary>
        /// 移动过程中修改绘制对象
        /// </summary>
        /// <param name="curPoint"></param>
        /// <param name="arrowAngle"></param>
        /// <param name="arrowLength"></param>
        public void DrawArrow(Point curPoint, double arrowAngle = Math.PI / 12, double arrowLength = 20)
        {
            // 获取待操作的 Canvas
            Canvas myCanvas = ArrowCanvas;
            if (myCanvas == null)
            {
                return;
            }
            // 修改多边形
            Polygon myPolygon = (Polygon)myCanvas.Children[0];
            double x1 = myPolygon.Points[0].X;
            double y1 = myPolygon.Points[0].Y;
            double x2 = curPoint.X;
            double y2 = curPoint.Y;
            Point point1 = new Point(x1, y1);     // 箭头起点
            Point point2 = new Point(x2, y2);     // 箭头终点            
            double angleOri = Math.Atan((y2 - y1) / (x2 - x1));      // 起始点线段夹角
            double angleDown = angleOri - arrowAngle;   // 箭头扩张角度
            double angleUp = angleOri + arrowAngle;     // 箭头扩张角度
            int directionFlag = (x2 > x1) ? -1 : 1;     // 方向标识
            double x3 = x2 + ((directionFlag * arrowLength) * Math.Cos(angleDown));   // 箭头第三个点的坐标
            double y3 = y2 + ((directionFlag * arrowLength) * Math.Sin(angleDown));
            double x4 = x2 + ((directionFlag * arrowLength) * Math.Cos(angleUp));     // 箭头第四个点的坐标
            double y4 = y2 + ((directionFlag * arrowLength) * Math.Sin(angleUp));
            Point point3 = new Point(x3, y3);   // 箭头第三个点
            Point point4 = new Point(x4, y4);   // 箭头第四个点
            myPolygon.Points[0] = new Point(x1, y1);
            myPolygon.Points[1] = new Point(x2, y2);
            myPolygon.Points[2] = new Point(x3, y3);
            myPolygon.Points[3] = new Point(x4, y4);
            myPolygon.Points[4] = new Point(x2, y2);
        }
    }
}
